package com.wing.order.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.order.model.response.DistributionDemandPlanVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.DistributionDemandPlan;
import com.wing.order.model.request.DistributionDemandPlanForm;
import com.wing.order.service.DistributionDemandPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-配货需求计划表管理", tags = "#/order/distributionDemandPlan")
@RestController("distributionDemandPlanController")
@RequestMapping("order/admin/distributionDemandPlan")
@RequiredArgsConstructor
public class DistributionDemandPlanController {

    private final DistributionDemandPlanService distributionDemandPlanService;


    @ApiOperation(value = "查询所有配货需求计划表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "status", value="状态：replay(1,待生成);done(2,已生成)", defaultValue = "status", example = "status", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "workshopName", value="所属车间", defaultValue = "workshopName", example = "workshopName", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCode", value="刀具柜编号", defaultValue = "cabCode", example = "cabCode", dataType = "String", paramType = "query")


    })
    @GetMapping("/list")
    public PageResult<DistributionDemandPlanVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,@RequestParam(defaultValue = "")String status
            ,@RequestParam(defaultValue = "")String workshopName
            ,@RequestParam(defaultValue = "")String cabCode
    ){

        Page<DistributionDemandPlanVO> pg = new Page<>(page, limit);
       /* QueryWrapper<DistributionDemandPlan> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(status)) {
            queryWrapper.eq("status", status);
        }
        if(StringUtils.isNotBlank(workshopName)){
            queryWrapper.eq("workshop_name",workshopName);
        }
        if(StringUtils.isNotBlank(cabCode)){
            queryWrapper.eq("cab_code",cabCode);
        }
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }*/

        //搜索
        //IPage<DistributionDemandPlan> list = distributionDemandPlanService.getBaseMapper().selectPage(pg, queryWrapper);
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(status)) {
            params.put("status", status);
        }
        if(StringUtils.isNotBlank(workshopName)){
            params.put("workshopName",workshopName);
        }
        if(StringUtils.isNotBlank(cabCode)){
            params.put("cabCode",cabCode);
        }
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        Page<DistributionDemandPlanVO> list = distributionDemandPlanService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 通过ID查询计划详情
     * @param planId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "计划主键ID", defaultValue = "", example = "plan_id", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/queryPlanById")
    public JsonResult queryPlanById(@RequestParam("planId") Integer planId){
        return JsonResult.success(distributionDemandPlanService.getById(planId));
    }


    @ApiOperation(value = "添加配货需求计划表", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(DistributionDemandPlanForm.Add.class) DistributionDemandPlanForm distributionDemandPlanForm) {
        if (distributionDemandPlanService.add(distributionDemandPlanForm) > 0) {


            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改配货需求计划表", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(DistributionDemandPlanForm.Update.class) DistributionDemandPlanForm distributionDemandPlanForm) {
        DistributionDemandPlan distributionDemandPlan = distributionDemandPlanService.getById(distributionDemandPlanForm.getId());
        if(distributionDemandPlan == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(DistributionDemandPlan.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(distributionDemandPlanForm, distributionDemandPlan, copyOptions);
        if (distributionDemandPlanService.updateById(distributionDemandPlan)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



@ApiOperation(value = "删除配货需求计划表", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "distributionDemandPlanId", value = "配货需求计划表id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long distributionDemandPlanId) {
        if (distributionDemandPlanService.removeById(distributionDemandPlanId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
